﻿@model IEnumerable<Common.Views.FaultView>
@{
    ViewBag.Title = "Update Fault Status";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Update Fault Status</h2>

<script type="text/javascript">

    function updatefaults(inc) {
        if ($("#ddlFaults" +inc+ "").val() == "") {
            alert("Choose a Fault Status to update the Fault!");
        }
        else {
            var oid = $('#orderid'+inc+'').val();
            var fid = $('#faultid'+inc+'').val();
            var fsdetails = $('#fdetails'+inc+'').val();
            var faults = $('#ddlFaults' +inc+ ' option:selected').val();
            var param = { OrderID: oid, faultID: fid, fDetails: fsdetails, statusId: faults };
            $.ajax({
                url: '@Url.Action("UpdateFaultStatus")',
                type: "POST",
                datatype: "json",
                data: param,
                error: function (xmlHttpRequest, errorText, thrownError) {
                    alert(xmlHttpRequest + "|" + "An error has occured. " + "|" + thrownError);
                },
                success: function (data) {
                    alert("Fault Status has been updated!");
                }
            });
        }
    }

</script>
@Html.Label("The actual text for the SMS will not be sent since credits will need to be purchased.") <br />
@if (Model.Count() > 0)
{
<table>
    <tr>
        <th>Username</th>
        <th>Product Name</th>
        <th>Product Image</th>
        <th>Fault Date</th>
        <th>Fault Details</th>
        <th>Fault Status</th>
        <th>Update Fault Status</th>      
    </tr>

    @{
        int inc = 0;
     }
@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Username)
            @Html.HiddenFor(modelItem => item.OrderID, new { id = "orderid"+inc+"" })
            @Html.HiddenFor(modelItem => item.FaultID, new { id = "faultid"+inc+"" })
            @Html.HiddenFor(modelItem => item.FaultDetails, new { id = "fdetails"+inc+"" })
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ProductName)
        </td>
        <td>
            <img height="150px" width="150px" src= "@Url.Content(item.ImageLink)" alt="@Html.DisplayFor(modelItem => item.ImageLink)" />
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FaultDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FaultDetails)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FaultStatus)
        </td>
        <td>
            @Html.DropDownListFor(model => item.FaultID, (IEnumerable<SelectListItem>)ViewData["faultList"], "--Select Fault Status--", new { id = "ddlFaults"+inc+""})
            <button type="button" onclick="updatefaults(@inc)">Update Fault Status</button>
        </td>
    </tr>
    {
        inc++; 
     }
}
</table>
}
else
{
    <br /> <div style="color:Red">@Html.Label("No products were found")</div>
}
